<?php

declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\validate\UserValidate;
use app\common\controller\BaseController;
use app\common\exception\AdminException;
use app\common\service\FriendsService;
use app\common\service\marketing\chest\FragmentExchangeLogService;
use app\common\service\post\PostsService;
use app\common\service\user\UserAssetService;
use app\common\service\user\UserService;
use app\common\service\UserPreferenceService;
use think\Request;

class User extends BaseController
{
    protected Request $request;

    public function __construct(UserService $userService, Request $request)
    {
        $this->service = $userService;
        $this->request = $request;
    }

    /**
     * 获取用户信息
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserInfo(): \think\Response
    {
        $user_id = $this->request->get('user_id');
        if(!$user_id) {
            $user_id = $this->request->userInfo['user_id'];
        }

        return $this->success($this->service->getUserInfo($user_id));
    }

    /**
     * 保存用户信息
     *
     * @param UserValidate $userValidate
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function saveUserInfo(UserValidate $userValidate): \think\Response
    {
        $user_id = $this->request->userInfo['user_id'];
        $data = $this->request->param();
        return $this->success($this->service->saveUserInfo($data, $user_id));
    }

    /**
     * 实名认证
     *
     * @param UserValidate $userValidate
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function realNameAuth(UserValidate $userValidate): \think\Response
    {
        $params = $this->request->post();
        $user_id = $this->request->userInfo['user_id'];
        if(!$userValidate->sceneidCard()->check($params)) {
            return $this->notValidate($userValidate->getError());
        }
        return $this->success($this->service->realNameAuth($user_id, $params['real_name'], $params['id_card']));
    }

    /**
     * 关注用户
     *
     * @return \think\Response
     */
    public function followUser(): \think\Response
    {
        $user_id = $this->request->userInfo['user_id'];
        $target_user_id = $this->request->post("target_user_id");
        if($target_user_id === $user_id) {
            throw new AdminException('不能关注自己');
        }
        $userPreferenceService = app()->make(UserPreferenceService::class);
        return $this->success($userPreferenceService->followUser($user_id, $target_user_id));
    }

    /**
     * 获取关注列表
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFollowingList(): \think\Response
    {
        $user_id = $this->request->userInfo['user_id'];
        $userPreferenceService = app()->make(UserPreferenceService::class);
        return $this->success($userPreferenceService->getFollowingList($user_id));
    }

    /**
     * 获取粉丝列表
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFollowersList(): \think\Response
    {
        $user_id = $this->request->userInfo['user_id'];
        $userPreferenceService = app()->make(UserPreferenceService::class);
        return $this->success($userPreferenceService->getFollowersList($user_id));
    }

    /**
     * 获取朋友列表
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function getFriendList(): \think\Response
    {
        $user_id = $this->request->userInfo['user_id'];
        $friendsService = app()->make(FriendsService::class);
        return $this->success($friendsService->getFriendList($user_id));
    }

    /**
     * 获取指定用户动态分页列表
     *
     * @return \think\Response
     * @throws \think\db\exception\DbException
     */
    public function getUserPostList(): \think\Response
    {
        $user_id = $this->request->get('user_id');
        $postService = app()->make(PostsService::class);
        return $this->success($postService->getUserPostList($user_id));
    }

    /**
     * 用户相互状态
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMutualState(): \think\Response
    {
        $user_id = $this->request->userInfo['user_id'];
        $target_user_id = $this->request->get('target_user_id');
        return $this->success($this->service->getMutualState($user_id, $target_user_id));
    }

    /**
     * 附近的人
     *
     * @param UserValidate $userValidate
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserList(UserValidate $userValidate): \think\Response
    {
        $params = $this->getParams();
        $user_id = $this->request->userInfo['user_id'];
        if(!$userValidate->heartbeat()->check($params)) {
            return $this->notValidate($userValidate->getError());
        }
        return $this->success($this->service->getUserList($user_id, $params));
    }

    /**
     * 获取用户背包列表
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function getUserAssetList(): \think\Response
    {
        $user_id = $this->request->userInfo['user_id'];
        $userAssetService = app()->make(UserAssetService::class);
        return $this->success($userAssetService->getUserAssetList($user_id));
    }

    /**
     * 获取用户兑换商品列表
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function getFragmentExchangeLogList(): \think\Response
    {
        $user_id = $this->request->userInfo['user_id'];
        $fragmentExchangeLogService = app()->make(FragmentExchangeLogService::class);
        return $this->success($fragmentExchangeLogService->getFragmentExchangeLogList($user_id));
    }
}